package cn.csg.iotplatform.module.system.api.project;

import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.module.system.api.project.dto.ProjectRespDTO;
import cn.csg.iotplatform.module.system.dal.dataobject.projectcopy.ProjectDO;
import cn.csg.iotplatform.module.system.dal.mysql.projectcopy.ProjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;
import cn.csg.iotplatform.module.system.api.project.dto.ProjectRespDTO;
import cn.csg.iotplatform.module.system.dal.dataobject.projectcopy.ProjectDO;
import cn.csg.iotplatform.module.system.service.project.ProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class ProjectApiImpl implements ProjectApi {

    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public CommonResult<List<ProjectRespDTO>> getProjectList(Collection<String> ids) {
        List<ProjectDO> projectDOS = projectService.selectProjectList(ids);
        return success(BeanUtils.toBean(projectDOS, ProjectRespDTO.class));
    }


    @Override
    public CommonResult<ProjectRespDTO> getProjetcInfoByProjectNo(String projectNo) {
        LambdaQueryWrapper<ProjectDO> projectDOLambdaQueryWrapper = new LambdaQueryWrapper<ProjectDO>().eq(ProjectDO::getProjectNo, projectNo);
        ProjectDO projectDO = projectMapper.selectOne(projectDOLambdaQueryWrapper);
        if (projectDO != null) {
            ProjectRespDTO projectRespDTO = new ProjectRespDTO();
            projectRespDTO.setId(projectDO.getId());
            projectRespDTO.setProjectNo(projectDO.getProjectNo());
            projectRespDTO.setProjectName(projectDO.getProjectName());
            projectRespDTO.setUnitCode(projectDO.getUnitCode());
            projectRespDTO.setUnitName(projectDO.getUnitName());
            projectRespDTO.setUnitUser(projectDO.getUnitUser());
            projectRespDTO.setUnitTel(projectDO.getUnitTel());
            projectRespDTO.setConstructionCode(projectDO.getConstructionCode());
            projectRespDTO.setConstructionName(projectDO.getConstructionName());
            projectRespDTO.setConstructionUser(projectDO.getConstructionUser());
            projectRespDTO.setConstructionTel(projectDO.getConstructionTel());
            projectRespDTO.setDataSource(projectDO.getDataSource());
            projectRespDTO.setEnablingStatus(projectDO.getEnablingStatus());
            projectRespDTO.setSubDeviceNum(projectDO.getSubDeviceNum());
            projectRespDTO.setCommonDeviceNum(projectDO.getCommonDeviceNum());
            projectRespDTO.setDescription(projectDO.getDescription());
            return success(projectRespDTO);
        }
        return success(null);
    }
}
